package guiClasses;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import objectClasses.Category;
import objectClasses.Database;
import objectClasses.Entry;
import objectClasses.User;
import objectClasses.UserInfo;

public class CategoryPanel extends GenericPanel
{
	private Category cat;
	
	public CategoryPanel(final Observer the_o_er, final String cat_name) 
	{
		super(the_o_er);
		update(the_o_er, cat_name);
	}
	
	public void update(final Observer the_o_er, final String cat_name)
	{
		cat = Category.getCategory(cat_name);
		final LinkedList<Entry> entries = cat.getEntries();
		
		if(GuiMain.getUser() != null)
		{
			if(GuiMain.getUser().getUserType() == 'J')
			{
				setWinnerButtons();
			}
			else if(GuiMain.getUser().getUserType() == 'O')
			{
				addRemButtons(the_o_er, cat_name);
			}
		}
		
		JPanel center = new JPanel(new GridBagLayout());
		JLabel cat_label = new JLabel("Category: " + cat.getName());
		Font f = new Font(cat_label.getFont().getName(),Font.BOLD, 20);  
		cat_label.setFont(f);
		center.add(cat_label, GBC.GBCGen(0,0,0,0));
		
		JPanel list = new JPanel(new GridBagLayout());
		JLabel contestant = new JLabel("Contestants");
		list.add(contestant, GBC.GBCGen(0,0,0,0));
		JLabel judge = new JLabel("Judges");
		list.add(judge, GBC.GBCGen(0,0,1,0));
		
		final ArrayList<UserInfo> judges = cat.getJudges();
		final ArrayList<UserInfo> contestants = cat.getContestants();
		JPanel contestant_p = new JPanel(new GridBagLayout());
		JPanel judge_p = new JPanel(new GridBagLayout());
		
		for(int i = 0; i < contestants.size(); i++)
		{
			JButton temp_u = new JButton(contestants.get(i).getUserName());
			temp_u.setForeground(Color.BLUE);
			temp_u.setBorderPainted(false);
			temp_u.setOpaque(false);
			temp_u.setBackground(new Color(139, 101, 219));
			final int t = i;
			temp_u.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent e) 
                {
                	setChanged();
    				notifyObservers(new InfoPanel(the_o_er, contestants.get(t), entries.get(t)));
                }
            });
			contestant_p.add(temp_u, GBC.GBCGen(0, 0, i/6, i % 6));
		}
		JScrollPane scrollPane = new JScrollPane(contestant_p);
		scrollPane.setPreferredSize(new Dimension(150, 190));
		list.add(scrollPane, GBC.GBCGen(0,0,0,1));
		
		JButton[] judge_b = new JButton[judges.size()];
		for(int i = 0; i < judges.size(); i++)
		{
			JButton temp_u = new JButton(judges.get(i).getUserName());
			temp_u.setForeground(Color.BLUE);
			temp_u.setBorderPainted(false);
			temp_u.setOpaque(false);
			temp_u.setBackground(new Color(139, 101, 219));
			final int t = i;
			temp_u.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent e) 
                {
                	setChanged();
    				notifyObservers(new InfoPanel(the_o_er, judges.get(t)).my_panel);
                }
            });
			temp_u.setCursor(new Cursor(Cursor.HAND_CURSOR));
			judge_b[i] = temp_u;
		}
		for(int i = 0; i < judges.size(); i++)
		{
			judge_p.add(judge_b[i], GBC.GBCGen(0, 0, i/6, i % 6));
		}
		JScrollPane temp = new JScrollPane(judge_p);
		temp.setPreferredSize(new Dimension(150, 190));
		list.add(temp, GBC.GBCGen(0,0,1,1));
		center.add(list, GBC.GBCGen(0,0,0,1));
		my_panel.add(center, GBC.GBCGen(0,0,1,1));
		my_panel.setBorder(new TitledBorder(cat_name + " Category"));
	}
	
	public void addRemButtons(final Observer the_o_er, final String cat_name)
	{
		JPanel add_rem = new JPanel(new GridBagLayout());
		JPanel buttons = new JPanel(new GridBagLayout());
		JLabel new_cat = new JLabel("Add/Remove Judge");
		add_rem.add(new_cat, GBC.GBCGen(0,0,0,0));
		final JTextField judge_name = new JTextField(12);
		add_rem.add(judge_name, GBC.GBCGen(0,0,0,1));
		
		JButton add_j = new JButton("Add");
		add_j.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	if(judge_name.getText().length()> 0)
            	{
            		try{
            		Database.DeleteAllUserEntries(judge_name.getText());
            		} catch (NullPointerException ex) {
            			
            		}
            		try 
            		{
						User.changeUserType('J', judge_name.getText());
					} 
            		catch (IOException e2) 
            		{
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
            		String temp[];
            		try 
            		{
            			char type = User.getType(judge_name.getText());
						temp = User.getUserInfo(judge_name.getText());
						UserInfo j = new UserInfo(type, temp[0], temp[1], temp[2], temp[3], temp[4]);
						cat.addJudges(j);
						setChanged();
						my_panel = new CategoryPanel(the_o_er, cat_name).my_panel;
						notifyObservers(my_panel);
					} 
            		catch (IOException e1) 
            		{
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
            	}
            }
        });
		buttons.add(add_j, GBC.GBCGen(0,0,0,0));
		
		JButton remove_j = new JButton("Remove");
		remove_j.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	try 
        		{
					User.changeUserType('A', judge_name.getText());
				} 
        		catch (IOException e2) 
        		{
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
            	String temp[];
            	char type = User.getType(judge_name.getText());
				try 
				{
					temp = User.getUserInfo(judge_name.getText());
					UserInfo j = new UserInfo(type, temp[0], temp[1], temp[2], temp[3], temp[4]);
					cat.removeJudge(j);
					setChanged();
					my_panel = new CategoryPanel(the_o_er, cat_name).my_panel;
					notifyObservers(my_panel);
				} 
				catch (IOException e1) 
				{
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
        });
		buttons.add(remove_j, GBC.GBCGen(0,0,1,0));
		add_rem.add(buttons, GBC.GBCGen(0,0,0,2));
		my_panel.add(add_rem, GBC.GBCGen(0,0,0,1));
	}
	
	public void setWinnerButtons()
	{
		JPanel set_win = new JPanel(new GridBagLayout());
		JPanel button = new JPanel(new GridBagLayout());
		JLabel new_cat = new JLabel("Choose Winner");
		set_win.add(new_cat, GBC.GBCGen(0,0,0,0));
		final JTextField contestant_name = new JTextField(12);
		set_win.add(contestant_name, GBC.GBCGen(0,0,0,1));
		
		JButton pick = new JButton("Pick");
		pick.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	if(contestant_name.getText().length()> 0)
            	{

            	}
            }
        });
		button.add(pick, GBC.GBCGen(0,0,0,0));
		
		set_win.add(button, GBC.GBCGen(0,0,0,2));
		my_panel.add(set_win, GBC.GBCGen(0,0,0,1));
	}
}
